<%@ include file="/WEB-INF/jsp/taglibs.jsp" %>
<%@ include file="/WEB-INF/jsp/header.jsp" %>
<%@ page import="sqs.model.*" %>
<%@ page import="java.util.*" %>

<html>
<head>
    <title><spring:message code="qnaire.heading"/></title>
</head>
<body>
    <blockquote>
        <form:form method="post" commandName="questionnaireForm" action="process.htm">
            
            <h1>${qnaire.questionnaireTitle}&nbsp;<spring:message code="qnaire.questionnaire"/></h1>
            
            <b><spring:message code="qnaire.description"/></b><br>${qnaire.description}<br><br>
            
            <%
                Questionnaire qnaire = (Questionnaire)pageContext.findAttribute("qnaire");
                List aList = (List)pageContext.findAttribute("ans");
                String incomplete = (String)pageContext.findAttribute("incomplete");
                
                List qList = qnaire.getQuestions();
            %>

            <c:if test="${not empty incomplete}">
                <h2>Mandatory questions have not been completed</h2>
            </c:if>
            
            <input type="hidden" name="QID" value="<%= qnaire.getQuestionnaireId() %>" />
            <table>
                <%
                    for(int i=0; i<qList.size(); i++) {
                        Question qn = (Question)qList.get(i);
                %>
                
                    <tr>
                        <td valign="top">Q<%= qn.getQuestionId() %>.</td>
                        <td valign="top"><%= qn.getQuestionText() %>
                            <% if(qn.isCompulsory()) out.println("(mandatory)"); %>
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>
                    
                    <% if((qn instanceof MCQQuestion) || (qn instanceof RatingQuestion)) {
                            String br = "";
                            if(qn.getLayout().equals(Question.VERTICAL_LAYOUT))
                                br = "<br>";

                            String ctrl = "radio";
                            if(qn instanceof MultipleAnsMCQQuestion)
                                ctrl = "checkbox";
                                
                            List oList = qn.getOptions();   
                            for (int j=0; j<oList.size(); j++) {
                                Option opt = (Option)oList.get(j); 
                             %>

                            <input 
                            <%
                                if(aList!=null) 
                                    if(aList.get(i)!=null)
                                        if(aList.get(i).equals(opt.getOptionId()+""))
                                            out.println("checked");
                            %>
                              type="<%= ctrl %>" 
                              name="Q<%= qn.getQuestionId() %>" 
                              value="<%= opt.getOptionId() %>"><%= opt.getOptionText() %>
                            </input><%= br %>
                            
                     <%    } 
                        } 
                        
                        if(qn instanceof NumericQuestion) {
                            String val = "0";
                            if(aList!=null) 
                                if(aList.get(i)!=null)
                                    val = (String)aList.get(i);
                            %>
                            <input 
                              type="text" 
                              name="Q<%= qn.getQuestionId() %>" 
                              value="<%= val %>" 
                              size="10" />
                     <%
                        }
                     %>

                    </td>
                    </tr>
                    <tr><td>&nbsp;</td></tr>
                <%
                    } 
                %>
            </table>
            
            <input type="submit" value="Submit" />
        </form:form>
    </blockquote>
</body>

<%@ include file="/WEB-INF/jsp/footer.jsp" %>
